package com.jdjc.auth.application.controller;

import com.google.common.base.Preconditions;
import com.jdjc.auth.application.convert.AuthPermissionDTOConverter;
import com.jdjc.auth.application.dto.AuthPermissionDTO;
import com.jdjc.auth.entity.Result;
import com.jdjc.auth.domin.entity.AuthPermissionBO;
import com.jdjc.auth.domin.service.AuthPermissionDominService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限Controller
 */
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {
    @Resource
    private AuthPermissionDominService authPermissionDominService;

    /**
     * 新增权限
     * @param authPermissionDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" PermissionController：add:dto:{}", authPermissionDTO);
            }
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。
            Preconditions.checkNotNull(authPermissionDTO.getName(),"权限名称不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(),"权限父id不能为空");
            //自定义类型装换
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            if (log.isInfoEnabled()) {
                log.info(" PermissionController：add:bo:{}", authPermissionBO);
            }
            //注册
            Boolean result = authPermissionDominService.add(authPermissionBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("PermissionController:add:error:{}", e.getMessage(), e);
            return Result.fail("新增权限失败");
        }

    }
    /**
     * 更新权限
     * @param authPermissionDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" PermissionController:update:dto:{}", authPermissionDTO);
            }
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。
            //Preconditions.checkNotNull(authPermissionDTO.getName(),"权限名称不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空");
            //自定义类型装换
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            if (log.isInfoEnabled()) {
                log.info(" PermissionController：update:bo:{}", authPermissionBO);
            }
            //注册
            Boolean result = authPermissionDominService.update(authPermissionBO);

            return Result.ok(result);
        } catch (Exception e) {
            log.error("PermissionController:add:error:{}", e.getMessage(), e);
            return Result.fail("更新权限失败");
        }

    }
    /**
     * 删除权限
     * @param authPermissionDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" PermissionController:delete:dto:{}", authPermissionDTO);
            }
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空");
            //自定义类型装换
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            if (log.isInfoEnabled()) {
                log.info(" PermissionController：delete:bo:{}", authPermissionBO);
            }
            Boolean result = authPermissionDominService.delete(authPermissionBO);

            return Result.ok(result);
        } catch (Exception e) {
            log.error("PermissionController:add:error:{}", e.getMessage(), e);
            return Result.fail("删除权限失败");
        }

    }
    /**
     * 查询用户权限
     * @param userName
     * @return
     */
    @RequestMapping("/getPermission")
    public Result<List<String>> getPermission(String userName) {
        try {
                log.info(" PermissionController:getPermission:userName:{}", userName);
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。
            Preconditions.checkArgument(!StringUtils.isBlank(userName),"用户名不能为空");
            List<String> result = authPermissionDominService.getPermission(userName);

            return Result.ok(result);
        } catch (Exception e) {
            log.error("PermissionController:/getPermission:error:{}", e.getMessage(), e);
            return Result.fail("查询权限失败");
        }

    }
}
